<template>
  <div class="login-wrap">

    <el-form label-position="left" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px"
      class="demo-ruleForm login-container">
      <h3 class="title">登陆</h3>
      <el-tabs v-model="activeName" >
        <el-tab-pane label="账号登录" name="first">
          <el-form-item prop="username">
            <el-input type="text" v-model="ruleForm.username" auto-complete="off" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="密码"></el-input>
          </el-form-item>
          <el-checkbox class="remember" v-model="rememberpwd">记住密码</el-checkbox>
          <el-form-item style="width: 100%">
            <el-button type="primary" style="width: 100%" @click="submitForm('ruleForm')"
              :loading="logining">登录</el-button>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="验证码登录" name="second">
          <el-form-item prop="mobile">
            <el-input type="text" v-model="ruleForm.mobile" auto-complete="off" placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-input placeholder="验证码" prefix-icon="el-icon-key" v-model="ruleForm.code">
              <template #append>
                <el-button @click="sendCode" :disabled="isSendcode ||contTime==0">{{contTime!=60?`重新发送(${contTime})`:'获取验证码'}}</el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-checkbox class="remember" v-model="rememberpwd">记住密码</el-checkbox>
          <el-form-item style="width: 100%">
            <el-button type="primary" style="width: 100%" @click="submitCode('ruleForm')"
              :loading="logining">登录</el-button>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>

    </el-form>
  </div>
</template>
<script t>
export default {
  name: "loginWrap",
  data() {
    return {
      activeName: 'first',
      logining: false,
      // 记住密码
      rememberpwd: false,
      formLabelWidth: "120px",
      ruleForm: {
        username: "",
        password: "",
        mobile:'',
        code:''
      },
      //rules前端验证
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      isSendcode:false,
      contTime:60
    };
  },

  // 创建完毕状态(里面是操作)
  created() { },
  method() { },
  // 里面的函数只有调用才会执行
  methods: {
    sendCode(){
      if(this.ruleForm.mobile){
        this.isSendcode=true
      const countdownInterval= setInterval(()=>{
        this.contTime--;
        if(this.contTime<=0){
          clearInterval(countdownInterval);
          this.isSendingCode= false;
          this.contTime=60
        }
      },1000)
      this.$http.post('/userInfo/sendCode',{mobile:this.ruleForm.mobile}).then(res=>{
        if (res.data.state != '200') {
          this.$message.error(res.info)
        }
        console.log(res);
      })
      }else{
        this.$message.error('请输入手机号')
      }
     
    },
    //登陆功能
    submitForm() {
      this.$http
        .post("/userInfo/userLogin", { account: this.ruleForm.username, passWord: this.ruleForm.password })
        .then((res) => {
          if (res.data.state == '200') {
            localStorage.setItem('userId', res.data.data.id)
            this.$router.push({ path: "/home" });
          }
          console.log(res);
        });
    },
     //登陆功能
     submitCode() {
      this.$http
        .post("/userInfo/loginByCode", { mobile: this.ruleForm.mobile, code: this.ruleForm.code })
        .then((res) => {
          if (res.data.state == '200') {
            localStorage.setItem('userId', res.data.data.id)
            this.$router.push({ path: "/home" });
          }
          console.log(res);
        });
    },
  },
};
</script>
  
<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.titleregister {
  margin: 0 auto;
  text-align: center;
  color: #0e7dec;
}

.remember {
  margin: 0px 0px 35px 0px;
}

.code-box {
  text-align: right;
}

.codeimg {
  height: 40px;
}
</style>
  